from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.prop_fields_def import prod_prop_pagnation_fields,base_prop_value_fields,prod_prop_fields
from services.online_mall.prod_prop_service import ProdPropService

class ProdPropValuesApi(Resource):
    @login_required
    @check_perms(perms=['prod:prop_value:list'])
    @marshal_with(base_prop_value_fields)
    def get(self,prop_id:str):
        return ProdPropService.get_prop_values(prop_id)
    
class ProdPropsApi(Resource):
    @login_required
    @check_perms(perms=['prod:prop:list'])
    @marshal_with(prod_prop_pagnation_fields)
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("is_sale_rule", type=StrBoolNone, required=False, default=True, location='args')
        parser.add_argument("page",type=int,required=False,default=-1,location='args')
        parser.add_argument("page_size",type=int,required=False,default=-1,location='args')
        args = parser.parse_args()
        shop_id = args.get("shop_id")
        is_sale_rule = args.get("is_sale_rule")
        page = args.get("page",-1)
        page_size = args.get("page_size",-1)
        return ProdPropService.get_prop_list(shop_id,is_sale_rule,page,page_size)
    
    @login_required
    @check_perms(perms=['prod:prop:create'])
    @marshal_with(prod_prop_fields)
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("is_sale_rule",type=StrBoolNone,required=False,default=True,location='json')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("prop_values",type=list,required=True,location='json')
        args = parser.parse_args()
        return ProdPropService.create_prop(current_user,args)
    
    @login_required
    @check_perms(perms=['prod:prop:update'])
    @marshal_with(prod_prop_fields)
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("is_sale_rule",type=StrBoolNone,required=False,default=True,location='json')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("prop_values",type=list,required=True,location='json')
        args = parser.parse_args()
        return ProdPropService.update_prop(current_user,args)
        
    @login_required
    @check_perms(perms=['prod:prop:del'])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='args')
        args = parser.parse_args()
        prop_id = args.get("id")
        return ProdPropService.delete_prop(current_user,prop_id)

# class ProdPropMaxIdApi(Resource):
#     @login_required
#     @check_perms(perms=['prod:prop:list'])
#     def get(self,prop_id:str):
#         return ProdPropService.get_prop_max_value_id(prop_id)

api.add_resource(ProdPropsApi, '/prod/prop')
api.add_resource(ProdPropValuesApi, '/prod/prop/<string:prop_id>/values')
# api.add_resource(ProdPropMaxIdApi, '/prod/prop/<string:prop_id>/max_id')